#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

Global $compare[4][8] = [ _
	[0,0,1,2,8,9,6,7], _
	[1,1,0,2,8,9,6,7], _
	[2,0,1,2,8,9,6,7], _
	[2,1,0,2,8,9,6,7] _
]

Func search_max_variant(ByRef $var, $var_cnt, $c, $skip, ByRef $log)
	Local $must = $compare[$c][0]
	Local $cmp[UBound($compare,2) - 1]
	For $ci = 0 To UBound($compare,2) - 2
		$cmp[$ci] = $compare[$c][$ci + 1]
	Next
	Local $max = -1
	Local $max_v
	Local $max_a
	For $v = 0 To $var_cnt - 1
		If $skip >= 0 And $skip = $v Then ContinueLoop
		Local $cur = $var[$v]
		Local $award = $cur[4]
		If $award[$must] > 0 Then
			If $max < 0 Or greater_award($award, $max_a, $cmp) Then
				$max = $v
				$max_v = $cur
				$max_a = $award
			EndIf
		EndIf
	Next
	Return $max
EndFunc

Func find_best_variant(ByRef $variant, $variant_cnt, ByRef $var, $var_cnt, $single, ByRef $log)
	Local $result[UBound($compare,1) * 2]
	Local $result_cnt = 0
	For $c = 0 To UBound($compare,1) - 1
		Local $max = search_max_variant($var, $var_cnt, $c, -1, $log)
		If $max < 0 Then ContinueLoop
		$result[$result_cnt] = $max
		$result_cnt += 1
		If $single Then ContinueLoop
		$max = search_max_variant($var, $var_cnt, $c, $max, $log)
		If $max < 0 Then ContinueLoop
		$result[$result_cnt] = $max
		$result_cnt += 1
	Next
	Local $add = 0
	For $c = 0 To $result_cnt - 1
		Local $new = True
		For $p = 0 To $c - 1
			If $result[$p] = $result[$c] Then
				$new = False
				ExitLoop
			EndIf
		Next
		If $new Then
			$add += 1
			$variant[$variant_cnt] = $var[$result[$c]]
			$variant_cnt += 1
		EndIf
	Next
	Return $add
EndFunc

func greater_award(ByRef $a1, ByRef $a2, ByRef $cmp)
	If $a1[3] >= 5  And $a2[3] < 5 Then
		Return True
	ElseIf $a2[3] >= 5 And $a1[3] < 5 Then
		Return False
	EndIf
	For $c = 0 To UBound($cmp) - 1
		Local $v1 = $a1[$cmp[$c]]
		Local $v2 = $a2[$cmp[$c]]
		If $v1 > $v2 Then
			Return True
		ElseIf $v1 < $v2 Then
			Return False
		EndIf
	Next
	Return False
EndFunc
